package com.tdam2011.grupo03.connection;

import java.io.BufferedReader;

import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HTTPInterface {

	private static StringBuilder responseStrR = null;
	private static StringBuilder responseStrS = null;
	private static StringBuilder responseStrC = null;
	private static String urls = "http://192.168.1.102:8080/MessageSender/";

	private static String user;

	public static String registrarUsuario(String us) {
		user = us;
		try {
			URL u = new URL(urls);
			URLConnection c = u.openConnection();
			c.setDoOutput(true);

			if (c instanceof HttpURLConnection) {
				((HttpURLConnection) c).setRequestMethod("POST");
			}

			OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
			out.write("<action id= 'REQUEST_RANDOM_VALUE' name= 'register-user'>");
			out.write("<action-detail>");
			out.write("<user username= '" + user + "' password= '123457'/>");
			out.write("</action-detail>");
			out.write("</action>");

			out.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					c.getInputStream()));
			responseStrR = new StringBuilder();
			String s = null;

			while ((s = in.readLine()) != null) {

				responseStrR.append(s + "\n");
			}
			in.close();

		} catch (Exception e) {
			e.printStackTrace();
			responseStrR = new StringBuilder();
			responseStrR.append("No connection");
		}
		return responseStrR.toString();
	}

	public static String enviarSms(String destino, String mensaje, String us) {
		try {
			user = us;
			URL u = new URL(urls);
			URLConnection c = u.openConnection();

			c.setDoOutput(true);
			if (c instanceof HttpURLConnection) {
				((HttpURLConnection) c).setRequestMethod("POST");
			}
			OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());

			out.write("<action id= 'REQUEST_RANDOM_VALUE' name= 'send-message'>");
			out.write("<action-detail>");
			out.write("<auth username= '" + user + "' key='123457'></auth>");
			out.write("<message to= '" + destino + "'><![CDATA[" + mensaje
					+ "]]></message>");
			out.write("</action-detail>");
			out.write("</action>");
			out.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					c.getInputStream()));
			responseStrS = new StringBuilder();
			String s = null;

			while ((s = in.readLine()) != null) {

				responseStrS.append(s + "\n");
			}
			in.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseStrS.toString();
	}

	public static String consultarSms(String us, String fecha) {
		try {
			URL u = new URL(urls);
			URLConnection c = u.openConnection();

			c.setDoOutput(true);
			if (c instanceof HttpURLConnection) {
				((HttpURLConnection) c).setRequestMethod("POST");
			}
			OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
			out.write("<action id= 'REQUEST_RANDOM_VALUE' name= 'get-messages'>");
			out.write("<action-detail>");
			out.write("<auth username= '" + us + "' key='123457'></auth>");
			out.write("<filter type= 'timestamp'> " + fecha + " </filter>");
			out.write("</action-detail>");
			out.write("</action>");

			out.close();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					c.getInputStream()));
			responseStrC = new StringBuilder();
			String s = null;

			while ((s = in.readLine()) != null) {

				responseStrC.append(s + "\n");
			}
			in.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return responseStrC.toString(); // devolver esto para el parser
										// responseStrC.toString() puse
		// la t para ver el mensaje en un toast
	}

	public static void setAliasRegistro(String a) {
		user = a;
	}

	public static String getAliasRegistro() {
		return user;
	}

}
